<?php

namespace app\common\model;

use think\Model;
use think\Env;
use traits\model\SoftDelete;
/**
 * 租赁表
 */
class Lease extends Model
{
    protected $name = "lease";
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    use SoftDelete;
    protected $deleteTime = 'deletetime';
    protected $updateTime = false;
    protected $append = [
        'status_text', //订单状态
        'card_text', 
        'card_list',//证件照,
        'endtime_text', //结束时间
        'express_text', //物流公司
    ];
    public function getStatusTextAttr($value, $data)
    {
        $status = $data['status'];
        switch ($status) {
            case '1':
                return '已下单';
            case '2':
                return '已发货';
            case '3':
                return '租用中';
            case '4':
                return '已归还';
            case '5':
                return '未评价';
            case '6':
                return '已完成';
        }
    }
    public function getCardTextAttr($value, $data)
    {

        $url = Env::get('site.url', config('site.url'));

        $thumbs_text = '';

        $thumbs = !empty($data['card']) ?  explode(',', $data['card']) : [];

        if (count($thumbs) > 0) {
            $thumbs_text = $url . $thumbs[0];
        } else {
            $thumbs_text = $url . '/assets/img/a.jpg';
        }

        return $thumbs_text;
    }
    public function getCardListAttr($value, $data)
    {

        $url = Env::get('site.url', config('site.url'));
        $thumbs_text = '';

        $thumbs = !empty($data['card']) ?  $data['card'] : [];

        if (!empty($thumbs)) {
            $thumbs = str_ireplace("/uploads/", $url . "/uploads/", $thumbs);
            $thumbs_text = explode(',', $thumbs);
        } else {
            $thumbs_text = [$url . '/assets/images/a.jpg'];
        }

        return $thumbs_text;
    }

    public function getEndtimeTextAttr($value, $data)
    {
        $endtime = !empty($data['endtime']) ? date('Y-m-d H:i:s',$data['endtime']) : '';

        return $endtime;
    }

    public function getExpressTextAttr($value, $data)
    {
        $expid = !empty($data['expid']) ? $data['expid'] : '';

        if ($expid) {
            $expressname = model('Expressquery')::where('id', $expid)->value('name');

            return $expressname;
        }
    }

    public function product()
    {
        return $this->belongsTo('app\common\model\Product\Product', 'proid', 'id', [], 'LEFT')->setEagerlyType(0);
  
    }
     //  区
     public function districts()
     {
         return $this->belongsTo('app\common\model\Region\Region', 'district', 'code', [], 'LEFT')->setEagerlyType(0);
     }
     //  市
     public function citys()
     {
         return $this->belongsTo('app\common\model\Region\Region', 'city', 'code', [], 'LEFT')->setEagerlyType(0);
     }
     //  省
     public function provinces()
     {
         return $this->belongsTo('app\common\model\Region\Region', 'province', 'code', [], 'LEFT')->setEagerlyType(0);
     }
    //  租户
     public function business()
     {
         return $this->belongsTo('app\common\model\Business\Business', 'busid', 'id', [], 'LEFT')->setEagerlyType(0);
     }
      // 审核员
    public function review()
    {
        return $this->belongsTo('app\common\model\Admin', 'adminid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
